package com.yishun.springboot.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.RequestGongWuShu.ElectricityMonitoring;
import com.alibaba.fastjson.JSONObject;
import com.yinquan.util.ResultUtils;
import com.yishun.springboot.mapper.ElectricityMonitoringGetdataMapper;
import com.yishun.springboot.mapper.GwsElectroMasterdetailedMapper;
import com.yishun.springboot.pojo.ElectricityMonitoringGetdata;
import com.yishun.springboot.pojo.GwsElectroMasterdetailed;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@Component
public class GongWuSuTimedUploadElectroService {

	@Resource
	GwsElectroMasterdetailedMapper gwsElectroMasterdetailedMapper;

	@Resource
	ElectricityMonitoringGetdataMapper electricityMonitoringGetdataMapper;

	public JSONObject addGwsElectro(GwsElectroMasterdetailed masterdetailed) {

		String api_key = masterdetailed.getApi_key();
		String eng_code = masterdetailed.getEng_code();
		String project_code = masterdetailed.getProject_code();

		String device_no = UUID.randomUUID().toString().replaceAll("-", "");

		masterdetailed.setDevice_no(device_no);

		JSONObject body = new JSONObject();
		body.put("device_no", device_no);
		body.put("type", masterdetailed.getType());
		body.put("name", masterdetailed.getName());
		body.put("install_company", masterdetailed.getInstall_company());
		body.put("installadd_type", masterdetailed.getInstalladd_type());
		body.put("temp_limit", masterdetailed.getTemp_limit());
		body.put("elec_limit", masterdetailed.getElec_limit());
		body.put("around_temp", masterdetailed.getAround_temp());

		String res = ElectricityMonitoring.uploadDetailed(body, api_key, eng_code, project_code);

		JSONObject jsonObject = JSONObject.parseObject(res);

		String code = jsonObject.getString("code");

		if (!code.equals("0")) {
			return ResultUtils.jsonFail("配电箱基础信息推送失败");
		}

		Integer res2 = gwsElectroMasterdetailedMapper.addGwsElectro(masterdetailed);

		if (res2 < 1) {
			return ResultUtils.jsonFail();
		}

		return ResultUtils.jsonSuccess();
	}

	@Async(value = "asyncExecutor")
	@Scheduled(fixedRate = 1000 * 30)
	public void uploadElectorData() {
		String api_key = null;
		String eng_code = null;
		String project_code = null;
		GwsElectroMasterdetailed masterdetailed = new GwsElectroMasterdetailed();

		// 将需要推送数据的配电箱设备全部查询出来
		List<GwsElectroMasterdetailed> masterdetaileds = gwsElectroMasterdetailedMapper.findByPushOrNotGetDeviceNo(1);

		for (int i = 0; i < masterdetaileds.size(); i++) {
			masterdetailed = masterdetaileds.get(i);

			api_key = masterdetailed.getApi_key();
			eng_code = masterdetailed.getEng_code();
			project_code = masterdetailed.getProject_code();

			// 通过编号查询出配电箱数据
			ElectricityMonitoringGetdata getdata = electricityMonitoringGetdataMapper
					.findByDeviceCode(masterdetailed.getDevice_code());

			if (getdata == null) {
				continue;
			}
			JSONObject body = new JSONObject();
			body.put("device_no", masterdetailed.getDevice_no());// 5e06e91f398f4a5388975ef1a1f596fb

			body.put("operate_time", DateUtil.now());
			// body.put("status", getdata.getStatus());
			body.put("status", 0);
			// body.put("warn_type", getdata.getWarn_type());
			body.put("warn_type", 0);
			body.put("cable_a_temp", getdata.getCable_a_tempm());
			body.put("cable_b_temp", getdata.getCable_b_tempm());
			body.put("cable_c_temp", getdata.getCable_c_tempm());
			// body.put("elec_limit", getdata.getElec_limit());
			body.put("elec_limit", RandomUtil.randomDouble(0.0, 300.0));
			body.put("env_temp", getdata.getEnv_temp());
			String res = ElectricityMonitoring.uploadData(body, api_key, eng_code, project_code);

			JSONObject jsonObject = JSONObject.parseObject(res);

			String code = jsonObject.getString("code");

			if (!code.equals("0")) {
				// log.info(masterdetailed.getDevice_no() + "推送实时数据失败。失败原因：" +
				// jsonObject.getString("message"));
				continue;
			}
			// log.info(masterdetailed.getDevice_no() + "推送实时数据成功");
		}

	}

}